package com.zhts.oa.department.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhts.oa.department.entity.Department;
import com.zhts.oa.department.entity.DeptTree;
import com.zhts.oa.department.mapper.DepartmentMapper;
import com.zhts.oa.department.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-12-10
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {


    @Override
    public List<DeptTree> getTree() {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq("dir_dept_code",0);
        List<Department> departments = this.list(wrapper);
        List<DeptTree> deptTrees = DeptTree.adpt(departments);
        if(deptTrees != null && deptTrees.size() > 0){
            deptTrees.forEach(this::childNode);
        }
        return deptTrees;
    }
    @Override
    public void childNode(DeptTree deptTree) {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq("dir_dept_code",deptTree.getDeptCode());
        List<Department> departments = this.list(wrapper);
        List<DeptTree> deptTrees = DeptTree.adpt(departments);
        deptTree.setDeptTrees(deptTrees);
        if(deptTrees != null && deptTrees.size() > 0){
            deptTrees.forEach(this :: childNode);
        }
    }
}
